package com.test.takephotodemo.takephotodemo;


import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.yanzhenjie.recyclerview.OnItemClickListener;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import com.yanzhenjie.recyclerview.touch.OnItemMoveListener;
import com.yanzhenjie.recyclerview.touch.OnItemMovementListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecyclerviewActivity extends BaseActivity {

    @BindView(R.id.srv)
    SwipeRecyclerView srv;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    private SRV_Adapter srv_adapter;

    private List<String> al;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview);
        ButterKnife.bind(this);
        initTestData();
    }

    public void initTestData() {
        al = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            al.add("这是测试数据的第" + i + "条,请查收");
        }
        srv_adapter = new SRV_Adapter(this, al);
        //默认listview的垂直布局
        srv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        // 横向(排成一排，不管有多少个,后面有个false和true)
        // srv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        //九宫格布局（垂直方向上的，3代表个数）
        // srv.setLayoutManager(new GridLayoutManager(this, 3));
        //九宫格布局（水平方向上的）
        // srv.setLayoutManager(new GridLayoutManager(this, 3, LinearLayoutManager.HORIZONTAL, false));
        // 点击事件
        srv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int adapterPosition) {
                Toast.makeText(getApplicationContext(), al.get(adapterPosition), Toast.LENGTH_SHORT).show();
            }
        });
        // 下拉刷新
        srl.setColorSchemeResources(R.color.colorPrimary);
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        al.clear();
                        for (int i = 0; i < 10; i++) {
                            al.add("这是测试数据的第" + i + "条,请查收");
                        }
                        srv_adapter.notifyDataSetChanged();
                        srl.setRefreshing(false);
                    }
                }, 500);
            }
        });
        // 使用默认的加载更多
        srv.useDefaultLoadMore();
        // 加载更多必须设置，这里有些坑。
        srv.loadMoreFinish(false, true);
        SwipeRecyclerView.LoadMoreListener mLoadMoreListener = new SwipeRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < 5; i++) {
                            al.add("这是测试数据的第" + i + "条,请查收");
                        }
                        srv_adapter.notifyDataSetChanged();
                        srv.loadMoreFinish(false, true);
                    }
                }, 500);
            }
        };
        // 加载更多添加
        srv.setLoadMoreListener(mLoadMoreListener);
        // 菜单创建器
        SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
                int height = ViewGroup.LayoutParams.MATCH_PARENT;
                SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext())
                        .setBackgroundColorResource(R.color.red)
                        .setText("删除") // 文字。
                        .setTextColor(Color.WHITE) // 文字颜色。
                        .setTextSize(16) // 文字大小。
                        .setWidth(200)// 宽高设置
                        .setHeight(height);
                swipeRightMenu.addMenuItem(deleteItem); // 添加菜单到右侧。
            }
        };
        // 添加菜单
        srv.setSwipeMenuCreator(swipeMenuCreator);
        // 菜单的item监听
        OnItemMenuClickListener mItemMenuClickListener = new OnItemMenuClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, int adapterPosition) {
                // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
                menuBridge.closeMenu();
                // 进行删除操作
                al.remove(adapterPosition);
                srv_adapter.notifyDataSetChanged();
            }
        };
        // 添加菜单的监听
        srv.setOnItemMenuClickListener(mItemMenuClickListener);
        // 拖拽设置
        srv.setLongPressDragEnabled(true);
        // 拖拽的监听
        OnItemMoveListener mItemMovementListener = new OnItemMoveListener() {
            @Override
            public boolean onItemMove(RecyclerView.ViewHolder srcHolder, RecyclerView.ViewHolder targetHolder) {
                // 此方法在Item拖拽交换位置时被调用。
                // 第一个参数是要交换为之的Item，第二个是目标位置的Item。
                // 交换数据，并更新adapter。
                int fromPosition = srcHolder.getAdapterPosition();
                int toPosition = targetHolder.getAdapterPosition();
                Collections.swap(al, fromPosition, toPosition);
                srv_adapter.notifyItemMoved(fromPosition, toPosition);
                return true;
            }

            @Override
            public void onItemDismiss(RecyclerView.ViewHolder srcHolder) {

            }
        };
        // 监听拖拽
        srv.setOnItemMoveListener(mItemMovementListener);


        // 设置适配器
        srv.setAdapter(srv_adapter);
    }
}